//
//  CDownLoadManager.h
//  Delicious
//
//  Created by yilong zhang on 11-11-21.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CNetWorkRequest.h"
#import "COperation.h"

@protocol TaskDelegate;
@class CNetWorkRequest;
@class COperation;

//=========================================================================================================================

typedef enum {Run, Wait, Over, Total}TaskStatus;

@interface CTask : NSObject

@property (nonatomic, retain)  NSString         *m_Url;
@property (nonatomic, retain)  id               m_Sponsor;
@property (nonatomic, retain)  id               m_Data;
@property (nonatomic, retain)  NSData           *m_PostData;
@property (nonatomic, retain)  id               m_UserInfo;
@property (nonatomic, assign)  int              m_nTimeOut;
@property (nonatomic, assign)  int              m_MType;
@property (nonatomic, assign)  TaskStatus       m_Status;

@property (nonatomic, assign)  COperation       *m_Operation;

-(id)initWithUrl:(NSString*)url Sponsor:(id)sponsor PostData:(NSData*)postdata TimeOut:(int)timeout Data:(id)data MType:(int)mtype  UserInfo:(id)userinfo;

@end

//=========================================================================================================================

@interface CDownLoadManager : NSObject <TaskDelegate>

@property (nonatomic, assign) NSOperationQueue      *m_OperationQueue;  //操作队列
@property (nonatomic, assign) NSMutableArray        *m_StoreQueue;
@property (nonatomic, assign) NSMutableArray        *m_FreeQueue;       //记录空闲操作ID
@property (nonatomic, assign) NSMutableArray        *m_TaskStack;       //任务等待队列
@property (nonatomic, assign) NSMutableArray        *m_TaskRunStack;    //正在处理的任务
@property (nonatomic, assign) int                   m_MaxNum;
@property (nonatomic, assign) NSCondition           *m_Lock;

+(CDownLoadManager*)DefaultDownLoadManager;

+(void)DestroyDownLoadManager;

-(id)initWithOperationNum:(int)MaxOpNum;

-(CTask*)AddTask:(NSString*)url Sponsor:(id)sponsor PostData:(NSData*)postdata TimeOut:(int)timeout Data:(id)data MType:(int)mtype UserInfo:(id)userinfo;

-(void)CancelTask:(CTask*)task;

-(void)CancelFinishedOperation:(COperation*)operation;

-(void)StartDownLoadTask;

-(void)StopDwonLoadTask;

@end
